package com.tronsis.library.util;

import android.app.Activity;
import android.content.Context;
import android.os.CountDownTimer;
import android.os.Handler;
import android.view.Gravity;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Toast工具类
 *
 * @author allen@tronsis.com
 * @date 7/5/2016 7:09 PM
 */
public class ToastUtil {

    private static String mOldMsg;
    protected static Toast mToast = null;
    private static long mFirstTime = 0;
    private static long mSecondTime = 0;
    private static void showMessage(Context context, String message, boolean inCenter) {
        if (context instanceof Activity && ((Activity) context).isFinishing()) {
            return;
        }

        if (mToast == null) {
            mToast = Toast.makeText(context.getApplicationContext(), message, Toast.LENGTH_SHORT);
            if (inCenter) {
                mToast.setGravity(Gravity.CENTER, 0, 0);
            }
            mToast.show();
            mFirstTime = System.currentTimeMillis();
        } else {
            mSecondTime = System.currentTimeMillis();
            if (inCenter) {
                mToast.setGravity(Gravity.CENTER, 0, 0);
            }
            if (message.equals(mOldMsg)) {
                if (mSecondTime - mFirstTime > Toast.LENGTH_SHORT) {
                    mToast.show();
                }
            } else {
                mOldMsg = message;
                mToast.setText(message);
                mToast.show();
            }
        }
        mFirstTime = mSecondTime;
    }

    /* */

    /**
     * 完全自定义布局Toast
     *//*
    public static void showMessage(Activity activity,String email) {
        if (mToast==null){
            mToast=new Toast(activity);
        }

        LayoutInflater inflater = activity.getLayoutInflater();
        LinearLayout toastLayout = (LinearLayout)inflater.inflate(R.layout.toast, null);
        TextView txtToast = (TextView)toastLayout.findViewById(R.id.my_view);
        txtToast.setText(email);
        mToast.setView(toastLayout);
        mToast.show();
        mToast=null;
    }*/
    public static void showMessage(Context context, int resId) {
        showMessage(context, context.getString(resId), false);
    }

    public static void showMessage(Context context, String message) {
        showMessage(context, message, false);
    }

    public static void showMessage(boolean isToast, Context context, String message) {
        if (isToast) {
            showMessage(context, message, false);
        }
    }

    public static void showToastInCenter(Context context, String content) {
        showMessage(context, content, true);
    }

    public static void showToastInCenter(Context context, int resId) {
        showMessage(context, context.getString(resId), true);
    }

    public static void showMyToast(final Toast toast, final int cnt) {
        final Timer timer =new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                toast.show();
            }
        },0,cnt);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                toast.cancel();
                timer.cancel();
            }
        }, cnt );
    }
}
